var myTemplate = Handlebars.compile($("#college-template").html());
var collegeList = [];
var bannerList = [];
var logoList = [];
var pageSize = 10;
var pageTotal = 0;
var pageIndex = 1;
var time = 0;
var type = 0;
var infoType = -1;
var categoryId = 0;
var userIds = 0;
$(function() {
    FastClick.attach(document.body);
    if ($.getPara("userId") != "") {
        var userId = $.getPara("userId");
        userIds = userId;
        $.setStorage("userId", userId)
    } else {
        if ($.getStorage("userId") && $.getStorage("userId") != "") {
            var userId = parseInt($.getStorage("userId"));
            userIds = userId
        }
    }
    type = $.getPara("type");
    categoryId = $.getPara("categoryId");
    getBanner();
    getList(type);
    infoType = $.getPara("infoType");
    // debugger;
    // console.log("infoType",infoType);
    if (infoType > 0) {
        getcollTypeList(1, infoType)
    } else {
        getcollList(1, time, categoryId, infoType)
    }
    $(".collTitle li").bind("click", function() {
        $(this).addClass("active").siblings().removeClass("active");
        var _index = $(this).index();
        time = $(this).find("a").attr("data-time");
        collegeList = [];
        $("#collList").html(myTemplate({
            data: collegeList
        }));
        pageIndex = 1;
        getcollList(pageIndex, time, categoryId, infoType)
    })
});
function pcDetails(id) {
    var para = {};
    para.title = "媒体详情";
    para.url = "/template/mediaDetails.html?id=" + id;
    if (userIds > 0) {
        para.url = "/template/mediaDetails.html?id=" + id + "&userId=" + userIds
    }
    para.type = "equEvaluating";
    if (/(Android)/i.test(navigator.userAgent)) {
        jsAndroid.appPush(JSON.stringify(para))
    } else {
        if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
            window.webkit.messageHandlers.appPush.postMessage(para)
        } else {
            window.location.href = para.url;
            alert("请使用移动设备！")
        }
    }
}
function getBanner() {
    $.ajax({
        type: "GET",
        url:  "/api/banner-ad/banner/index",
        data: {
            type: 4
        },
        success: function(res) {
            if (res.code == 200) {
                var list = res.data;
                $.each(list, function(index, val) {
                    if (val.url_type == 0) {
                        bannerList += '<div class="swiper-slide"><a href="javascript:;"><img src="' + val.img + '"/></a></div>'
                    } else {
                        bannerList += '<div class="swiper-slide"><a onclick="pcDetails(' + val.url_value + ')" href="javascript:;"><img src="' + val.img + '"/></a></div>'
                    }
                });
                $("#bannerSwiper").html(bannerList);
                var swiper = new Swiper("#swiper-banber",{
                    autoplay: 3000,
                    loop: true,
                    pagination: "#pagination-banner",
                    autoplayDisableOnInteraction: false
                })
            } else {
                $.toast("请求失败", "text")
            }
        }
    })
}
function urlHref(id, type) {
    var para = {};
    para.title = "媒体列表";
    para.type = "topic";
    para.url = "/template/media.html?categoryId=" + id + "&type= " + type;
    if (userIds > 0) {
        para.url = "/template/media.html?categoryId=" + id + "&type= " + type + "&userId=" + userIds
    }
    if (/(Android)/i.test(navigator.userAgent)) {
        jsAndroid.appPush(JSON.stringify(para))
    } else {
        if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
            window.webkit.messageHandlers.appPush.postMessage(para)
        } else {
            window.location.href = para.url;
            alert("PC浏览器")
        }
    }
}
function getList(type) {
    var _type = type;
    $.ajax({
        type: "GET",
        url:  "/api/media/category/listLimit",
        data: {
            limitNum: 999,
            type: _type,
        },
        success: function(res) {
            if (typeof (res) == "string") {
                res = JSON.parse(res)
            }
            if (res.code == 200) {
                var list = res.data;
                var num = 12;
                var lie = [];
                for (var i = 0; i < list.length; i++) {
                    lie.push(list[i]);
                    if ((i != 0 && (i + 1) % num == 0) || i == list.length - 1) {
                        var para = {};
                        para.list = lie;
                        logoList.push(para);
                        lie = []
                    }
                }
                $("#collegewrapper").html(myTemplate({
                    data: logoList
                }));
                collegewrapper()
            } else {
                $.toast("请求失败", "text")
            }
        }
    })
}
function collegewrapper() {
    var Logoswiper = new Swiper("#collegeSwiper",{
        autoplay: 4000,
        loop: true,
        pagination: "#paginationSwiper",
        autoplayDisableOnInteraction: false
    })
}
function urlHrefDetails(id) {
    var para = {};
    para.title = "媒体详情";
    para.type = "topic";
    para.url = "/template/mediaDetails.html?id=" + id;
    if (userIds > 0) {
        para.url = "/template/mediaDetails.html?id=" + id + "&userId=" + userIds
    }
    if (/(Android)/i.test(navigator.userAgent)) {
        jsAndroid.appPush(JSON.stringify(para))
    } else {
        if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
            window.webkit.messageHandlers.appPush.postMessage(para)
        } else {
            window.location.href = para.url;
            alert("PC浏览器")
        }
    }
}
function getcollList(pageIndex, time, categoryId, infoType) {
    $("#pageLoading").show();
    var pageIndex = pageIndex;
    var _categoryId = categoryId;
    var _type = infoType;
    var time = time;
    $.ajax({
        type: "GET",
        url:  "/api/media/category/info/list",
        data: {
            categoryId: _categoryId,
            type: _type,
            timeType: time,
            pageIndex: pageIndex,
            pageSize: pageSize,
            
        },
        success: function(res) {
            if (res.code == 200) {
                var list = res.data.list;
                pageTotal = res.data.total;
                if (list == "") {
                    $(document.body).destroyInfinite();
                    $("#pageNomore").show()
                } else {
                    collegeList = collegeList.concat(list);
                    var myTemplate = Handlebars.compile($("#collList-template").html());
                    $("#collList").html(myTemplate({
                        data: collegeList
                    }))
                }
                $("#pageLoading").hide();
                $(document.body).infinite()
            } else {
                $.toast("请求失败", "text")
            }
        }
    })
}
function getcollTypeList(pageIndex, infoType) {
    $("#pageLoading").show();
    var pageIndex = pageIndex;
    var _type = infoType;
    $.ajax({
        type: "GET",
        url:  "/api/media/type/info/list",
        data: {
            type: _type,
            pageIndex: pageIndex,
            pageSize: pageSize,
        },
        success: function(res) {
            if (res.code == 200) {
                var list = res.data.list;
                pageTotal = res.data.total;
                if (list == "") {
                    $(document.body).destroyInfinite();
                    $("#pageNomore").show()
                } else {
                    collegeList = collegeList.concat(list);
                    var myTemplate = Handlebars.compile($("#collList-template").html());
                    $("#collList").html(myTemplate({
                        data: collegeList
                    }))
                }
                $("#pageLoading").hide();
                $(document.body).infinite()
            } else {
                $.toast("请求失败", "text")
            }
        }
    })
}
$(document.body).infinite(1).on("infinite", function() {
    var pageNum = Math.ceil(divideNumber(pageTotal, pageSize));
    if (pageIndex < pageNum) {
        $(document.body).destroyInfinite();
        pageIndex = ++pageIndex;
        if (infoType > 0) {
            getcollTypeList(pageIndex, infoType)
        } else {
            getcollList(pageIndex, time, categoryId, infoType)
        }
      
    } else {
        $(document.body).destroyInfinite();
        $("#pageNomore").show()
    }
});
